package com.weiaibenpao.demo.chislim.utils;

import android.app.Activity;
import android.content.Context;
import android.support.annotation.NonNull;
import android.widget.Toast;

import com.umeng.socialize.ShareAction;
import com.umeng.socialize.UMShareListener;
import com.umeng.socialize.bean.SHARE_MEDIA;
import com.umeng.socialize.media.UMImage;
import com.umeng.socialize.media.UMWeb;
import com.weiaibenpao.demo.chislim.R;
import com.weiaibenpao.demo.chislim.api.Url;

import static com.umeng.socialize.bean.SHARE_MEDIA.QQ;

/**
 * Created by wlx on 2017/12/7.
 */

public class ShareUtils {
    private static final String SHARE_SUFFIX = "&shareJudge=y";
//    private Listener mListener;
    private Context mContext;

    public ShareUtils( Context mContext) {
        this.mContext = mContext;
    }

    public void share( String url,String title){
        UMWeb web = new UMWeb(Url.BASE_URL+Url.SERVICE_SPORT+url);
        web.setTitle(title);//标题
        web.setThumb(new UMImage(mContext, R.mipmap.ic_logo));  //缩略图
        web.setDescription(mContext.getString(R.string.default_share_content));//描述
        new ShareAction((Activity) mContext)
                .setDisplayList(getShareMedias())
                .setCallback(shareListener)
                .withMedia(web)
                .open();
    }
    public void shareSport(String url, String title){
        UMWeb web = new UMWeb(Url.BASE_URL+url);
        web.setTitle(title);//标题
        web.setThumb(new UMImage(mContext, R.mipmap.ic_logo));  //缩略图
        web.setDescription(mContext.getString(R.string.default_share_content));//描述
        new ShareAction((Activity) mContext)
                .setDisplayList(getShareMedias())
                .setCallback(shareListener)
                .withMedia(web)
                .open();
    }
//    public void setListener(Listener listener){
//        this.mListener=listener;
//    }
//    public interface Listener {
//        void shareSuccess();
//
//        void shareFail(String msg);
//
//        void shareCancel();
//    }

    private UMShareListener shareListener = new UMShareListener() {
        /**
         * @descrption 分享开始的回调
         * @param platform 平台类型
         */
        @Override
        public void onStart(SHARE_MEDIA platform) {
        }

        /**
         * @descrption 分享成功的回调
         * @param platform 平台类型
         */
        @Override
        public void onResult(SHARE_MEDIA platform) {
//            mListener.shareSuccess();
        }

        /**
         * @descrption 分享失败的回调
         * @param platform 平台类型
         * @param t 错误原因
         */
        @Override
        public void onError(SHARE_MEDIA platform, Throwable t) {
//          mListener.shareFail(t.getMessage());
            Toast.makeText(mContext,t.getMessage(),Toast.LENGTH_SHORT).show();

        }

        /**
         * @descrption 分享取消的回调
         * @param platform 平台类型
         */
        @Override
        public void onCancel(SHARE_MEDIA platform) {
//          mListener.shareCancel();
            Toast.makeText(mContext,"取消分享",Toast.LENGTH_SHORT).show();

        }
    };
    @NonNull
    private SHARE_MEDIA[] getShareMedias() {
        return new SHARE_MEDIA[]
                {
                        SHARE_MEDIA.WEIXIN, SHARE_MEDIA.WEIXIN_CIRCLE, SHARE_MEDIA.SINA,
                        QQ, SHARE_MEDIA.QZONE
                };
    }
}
